#
# Copyright (C) 2000-2023 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#
# Bacula Tools Makefile
#
@MCOMMON@

TOKYOCABINET_INC  = @TOKYOCABINET_INC@
TOKYOCABINET_LIBS = @TOKYOCABINET_LIBS@

srcdir =	.
VPATH = 	.
.PATH:		.

# one up
basedir = ..
# top dir
topdir = ../..
# this dir relative to top dir
thisdir = src/tools

DEBUG=@DEBUG@

ZLIBS=@ZLIBS@
DB_LIBS=@DB_LIBS@

first_rule: all
dummy:

#

GETTEXT_LIBS = @LIBINTL@

FINDOBJS = testfind.o ../dird/dird_conf.o ../dird/inc_conf.o ../dird/run_conf.o ../dird/ua_acl.o

# these are the objects that are changed by the .configure process
EXTRAOBJS = @OBJLIST@

DIRCONFOBJS = ../dird/dird_conf.o ../dird/run_conf.o ../dird/inc_conf.o ../dird/ua_acl.o

NODIRTOOLS = bsmtp bjoblist
DIRTOOLS = bsmtp dbcheck drivetype fstype testfind testls bregex bwild bbatch bregtest bvfs_test bjoblist
TOOLS = $(@DIR_TOOLS@)

INSNODIRTOOLS = bsmtp
INSDIRTOOLS = bsmtp dbcheck bwild bregex
INSTOOLS = $(INS@DIR_TOOLS@) @TOOLS_INSTALL@
INSTTESTS = breaddir_test bpipe_test tags_test xattr_append_test crypto_test

.SUFFIXES:	.c .o
.PHONY:
.DONTCARE:

# inference rules
.c.o:
	@echo "Compiling $<"
	$(NO_ECHO)$(CXX) $(DEFS) $(DEBUG) -c $(CPPFLAGS) -I$(srcdir) -I$(basedir) $(DINCLUDE) $(CFLAGS) $<
#-------------------------------------------------------------------------
all: Makefile $(TOOLS) gigaslam grow
	@echo "==== Make of tools is good ===="
	@echo " "

../lib/unittests.o: ../lib/unittests.c
	(cd ../lib ; make unittests.o)

test-cpp: test-cpp.o ../lib/unittests.o
	$(LIBTOOL_LINK) $(CXX) $(LDFLAGS)  -L../lib -o $@ test-cpp.o ../lib/unittests.o -lbac $(DLIB) $(LIBS) $(GETTEXT_LIBS) $(OPENSSL_LIBS)

debug-tags_test: debug-tags.o ../lib/unittests.o
	$(LIBTOOL_LINK) $(CXX) $(LDFLAGS)  -L../lib -o $@ debug-tags.o ../lib/unittests.o -lbac $(DLIB) $(LIBS) $(GETTEXT_LIBS) $(OPENSSL_LIBS)
	$(LIBTOOL_INSTALL) $(INSTALL_PROGRAM) $@ $(DESTDIR)$(sbindir)/

fs-io-error: fs-io-error.c
	$(CXX) -Wall fs-io-error.c -D_FILE_OFFSET_BITS=64 -lfuse  -o fs-io-error

breaddir_test: breaddir.o ../lib/unittests.o
	$(LIBTOOL_LINK) $(CXX) $(LDFLAGS)  -L../lib -o $@ breaddir.o ../lib/unittests.o -lbac $(DLIB) $(LIBS) $(GETTEXT_LIBS) $(OPENSSL_LIBS)
	$(LIBTOOL_INSTALL) $(INSTALL_PROGRAM) $@ $(DESTDIR)$(sbindir)/

joblist_test: joblist.o
	make -C ../lib/ alist_test
	$(LIBTOOL_LINK) $(CXX) $(LDFLAGS)  -L../lib -L../findlib -o $@ joblist.o ../lib/unittests.o -lbac -lbacfind $(DLIB) $(LIBS) $(GETTEXT_LIBS) $(OPENSSL_LIBS)
	$(LIBTOOL_INSTALL) $(INSTALL_PROGRAM) $@ $(DESTDIR)$(sbindir)/

bjoblist: bjoblist.o
	$(LIBTOOL_LINK) $(CXX) $(LDFLAGS)  -L../lib -L../findlib -o $@ bjoblist.o -lbac -lbacfind $(DLIB) $(LIBS) $(GETTEXT_LIBS) $(OPENSSL_LIBS)

bsock_meeting_test: bsock_meeting_test.o ../lib/unittests.o
	$(LIBTOOL_LINK) $(CXX) $(LDFLAGS)  -L../lib -o $@ bsock_meeting_test.o ../lib/unittests.o -lbac $(DLIB) $(LIBS) $(GETTEXT_LIBS) $(OPENSSL_LIBS)

xattr_append_test: xattr_append_test.o ../lib/unittests.o
	$(LIBTOOL_LINK) $(CXX) $(LDFLAGS)  -L../lib -o $@ xattr_append_test.o ../lib/unittests.o -lbac $(DLIB) $(LIBS) $(GETTEXT_LIBS) $(OPENSSL_LIBS)

bpipe_test: bpipe_test.o ../lib/unittests.o
	$(LIBTOOL_LINK) $(CXX) $(LDFLAGS)  -L../lib -o $@ bpipe_test.o ../lib/unittests.o -lbac $(DLIB) $(LIBS) $(GETTEXT_LIBS) $(OPENSSL_LIBS)
	$(LIBTOOL_INSTALL) $(INSTALL_PROGRAM) $@ $(DESTDIR)$(sbindir)/

crypto_test: crypto_test.o ../lib/unittests.o
	$(LIBTOOL_LINK) $(CXX) $(LDFLAGS)  -L../lib -o $@ crypto_test.o ../lib/unittests.o -lbac $(DLIB) $(LIBS) $(GETTEXT_LIBS) $(OPENSSL_LIBS)

bsparse: bsparse.c
	$(CXX) -Wall -o $@ bsparse.c

bsmtp: Makefile bsmtp.o ../lib/libbac$(DEFAULT_ARCHIVE_TYPE)	    
	$(LIBTOOL_LINK) $(CXX) $(LDFLAGS) -L../lib -o $@ bsmtp.o -lbac -lm $(DLIB) $(LIBS) $(GETTEXT_LIBS) $(OPENSSL_LIBS)

bsnapshot: Makefile bsnapshot.o ../lib/libbac$(DEFAULT_ARCHIVE_TYPE)
	$(LIBTOOL_LINK) $(CXX) $(LDFLAGS) -L../lib -o $@ bsnapshot.o ../lib/ini$(DEFAULT_OBJECT_TYPE) -lbac -lm $(DLIB) $(LIBS) $(GETTEXT_LIBS)


bregtest: Makefile bregtest.o ../lib/libbac$(DEFAULT_ARCHIVE_TYPE)	   
	$(LIBTOOL_LINK) $(CXX) $(LDFLAGS) -L../lib -o $@ bregtest.o -lbac -lm $(DLIB) $(LIBS) $(GETTEXT_LIBS) $(OPENSSL_LIBS)

hdtest: Makefile hdtest.o ../lib/libbac$(DEFAULT_ARCHIVE_TYPE)	   
	$(LIBTOOL_LINK) $(CXX) $(LDFLAGS) -L../lib -o $@ hdtest.o -lbac -lm $(DLIB) $(LIBS)

thread: Makefile thread.o 
	$(CXX) $(LDFLAGS) -o $@ thread.o -lpthread

dbcheck: Makefile dbcheck.o ../lib/libbac$(DEFAULT_ARCHIVE_TYPE) ../lib/libbaccfg$(DEFAULT_ARCHIVE_TYPE) \
	 ../cats/libbacsql$(DEFAULT_ARCHIVE_TYPE) ../cats/libbaccats$(DEFAULT_ARCHIVE_TYPE) $(DIRCONFOBJS)
	$(LIBTOOL_LINK) $(CXX) $(LDFLAGS) -L../lib -L../cats -L../findlib -o $@ dbcheck.o $(DIRCONFOBJS) \
	  -lbaccats -lbacsql -lbaccfg -lbac -lbacfind -lm $(ZLIBS) $(DB_LIBS) $(LIBS) $(GETTEXT_LIBS) $(OPENSSL_LIBS)

fstype: Makefile fstype.o ../lib/libbac$(DEFAULT_ARCHIVE_TYPE) ../findlib/libbacfind$(DEFAULT_ARCHIVE_TYPE)
	$(LIBTOOL_LINK) $(CXX) $(LDFLAGS) -L../lib -L../findlib -o $@ fstype.o -lbacfind -lbac -lm \
	  $(DLIB) $(LIBS) $(GETTEXT_LIBS) $(OPENSSL_LIBS)

bnet_test: Makefile bnet_test.o ../lib/libbac$(DEFAULT_ARCHIVE_TYPE) ../findlib/libbacfind$(DEFAULT_ARCHIVE_TYPE)
	$(LIBTOOL_LINK) $(CXX) $(LDFLAGS) -L../lib -L../findlib -o $@ bnet_test.o -lbacfind -lbac -lm \
	  $(DLIB) $(LIBS) $(GETTEXT_LIBS) $(OPENSSL_LIBS)

block_crypto_test: Makefile block_crypto_test.o ../lib/libbac$(DEFAULT_ARCHIVE_TYPE) ../lib/unittests.o
	$(LIBTOOL_LINK) $(CXX) $(LDFLAGS) -L../lib -o $@ block_crypto_test.o ../lib/unittests.o -lbac -lm \
	  $(DLIB) $(LIBS) $(GETTEXT_LIBS) $(OPENSSL_LIBS)
	$(LIBTOOL_INSTALL) $(INSTALL_PROGRAM) $@ $(DESTDIR)$(sbindir)/

tags_test: Makefile test_tags.o ../lib/libbac$(DEFAULT_ARCHIVE_TYPE) ../lib/unittests.o
	$(LIBTOOL_LINK) $(CXX) $(LDFLAGS) -L../lib -o $@ test_tags.o ../lib/unittests.o -lbac \
	  $(DLIB) $(LIBS)
	$(LIBTOOL_INSTALL) $(INSTALL_PROGRAM) $@ $(DESTDIR)$(sbindir)/

drivetype: Makefile drivetype.o ../lib/libbac$(DEFAULT_ARCHIVE_TYPE) ../findlib/libbacfind$(DEFAULT_ARCHIVE_TYPE)
	$(LIBTOOL_LINK) $(CXX) $(LDFLAGS) -L../lib -L../findlib -o $@ drivetype.o -lbacfind -lbac -lm \
	  $(DLIB) $(LIBS) $(GETTEXT_LIBS) $(OPENSSL_LIBS)

dird_conf.o: ../dird/dird_conf.c
	$(CXX) $(DEFS) $(DEBUG) -c $(CPPFLAGS) -I$(srcdir) -I$(basedir) $(DINCLUDE) $(CFLAGS) $<

run_conf.o: ../dird/run_conf.c
	$(CXX) $(DEFS) $(DEBUG) -c $(CPPFLAGS) -I$(srcdir) -I$(basedir) $(DINCLUDE) $(CFLAGS) $<

inc_conf.o: ../dird/inc_conf.c
	$(CXX) $(DEFS) $(DEBUG) -c $(CPPFLAGS) -I$(srcdir) -I$(basedir) $(DINCLUDE) $(CFLAGS) $<

timelimit: timelimit.o
	${CC} ${DEFS} ${DEBUG} -pipe -DHAVE_ERRNO_H -DHAVE_SETITIMER -DHAVE_SIGACTION -c timelimit.c 
	${CC} -o timelimit timelimit.o

testfind: Makefile ../lib/libbac$(DEFAULT_ARCHIVE_TYPE) ../lib/libbaccfg$(DEFAULT_ARCHIVE_TYPE) \
	  ../findlib/libbacfind$(DEFAULT_ARCHIVE_TYPE) $(FINDOBJS)
	$(LIBTOOL_LINK) $(CXX) -g $(LDFLAGS) -o $@ $(FINDOBJS) -L. -L../lib -L../findlib \
	  $(DLIB) -lbacfind -lbaccfg -lbac -lm $(LIBS) $(GETTEXT_LIBS) $(OPENSSL_LIBS)

testls: Makefile ../findlib/libbacfind$(DEFAULT_ARCHIVE_TYPE) ../lib/libbac$(DEFAULT_ARCHIVE_TYPE) testls.o
	$(LIBTOOL_LINK) $(CXX) -g $(LDFLAGS) -L. -L../lib -L../findlib -o $@ testls.o \
	  $(DLIB) -lbacfind -lbac -lm $(LIBS) $(GETTEXT_LIBS) $(OPENSSL_LIBS)

bregex: Makefile ../findlib/libbacfind$(DEFAULT_ARCHIVE_TYPE) ../lib/libbac$(DEFAULT_ARCHIVE_TYPE) bregex.o
	$(LIBTOOL_LINK) $(CXX) -g $(LDFLAGS) -L. -L../lib -o $@ bregex.o \
	  $(DLIB) -lbac -lm $(LIBS) $(GETTEXT_LIBS) $(OPENSSL_LIBS)

bwild:	Makefile ../lib/libbac$(DEFAULT_ARCHIVE_TYPE) bwild.o
	$(LIBTOOL_LINK) $(CXX) -g $(LDFLAGS) -L. -L../lib -o $@ bwild.o \
	  $(DLIB) -lbac -lm $(LIBS) $(GETTEXT_LIBS) $(OPENSSL_LIBS)

bbatch: Makefile ../lib/libbac$(DEFAULT_ARCHIVE_TYPE) ../cats/libbacsql$(DEFAULT_ARCHIVE_TYPE) \
	../cats/libbaccats$(DEFAULT_ARCHIVE_TYPE) bbatch.o
	$(LIBTOOL_LINK) $(CXX) -g $(LDFLAGS) -L../cats -L. -L../lib -L../findlib -o $@ bbatch.o \
	  -lbaccats -lbacsql -lbac -lbacfind -lm $(ZLIBS) $(DB_LIBS) $(LIBS) $(GETTEXT_LIBS) $(OPENSSL_LIBS)

bvfs_test: Makefile ../findlib/libbacfind$(DEFAULT_ARCHIVE_TYPE) ../lib/libbac$(DEFAULT_ARCHIVE_TYPE) \
	   ../cats/libbacsql$(DEFAULT_ARCHIVE_TYPE) ../cats/libbaccats$(DEFAULT_ARCHIVE_TYPE) bvfs_test.o
	$(LIBTOOL_LINK) $(CXX) -g $(LDFLAGS) -L../cats -L. -L../lib -L../findlib -o $@ bvfs_test.o  \
	  -lbaccats -lbacsql -lbacfind -lbac -lm $(ZLIBS) $(DB_LIBS) $(LIBS) $(GETTEXT_LIBS) $(OPENSSL_LIBS)

# Turn on cats_test when it builds correctly
#cats_test.o: cats_test.c
#	 echo "Compiling $<"
#	 $(NO_ECHO)$(CXX) $(DEFS) $(DEBUG) -c $(CPPFLAGS) -I$(srcdir) -I$(basedir) $(DINCLUDE) $(CFLAGS) $<

#cats_test: Makefile ../findlib/libbacfind$(DEFAULT_ARCHIVE_TYPE) ../lib/libbac$(DEFAULT_ARCHIVE_TYPE) ../cats/libbacsql$(DEFAULT_ARCHIVE_TYPE) cats_test.o
#	 $(LIBTOOL_LINK) $(CXX) -g $(LDFLAGS) -L../cats -L. -L../lib -L../findlib -o $@ cats_test.o  \
#	   -lbaccats -lbacsql -lbacfind -lbac -lm $(DB_LIBS) $(LIBS) $(GETTEXT_LIBS) $(OPENSSL_LIBS)

gigaslam.o: gigaslam.c
	$(CXX) $(CFLAGS) -c $(CPPFLAGS) $<

gigaslam: gigaslam.o
	$(LIBTOOL_LINK) $(CXX) $(LDFLAGS) -o $@ gigaslam.o

grow: Makefile grow.o ../lib/libbac$(DEFAULT_ARCHIVE_TYPE)
	$(LIBTOOL_LINK) $(CXX) $(LDFLAGS) -L../lib -o $@ grow.o -lbac -lm $(DLIB) $(LIBS) $(GETTEXT_LIBS) $(OPENSSL_LIBS)

bpluginfo.o: bpluginfo.c
	$(CXX) $(DEFS) $(DEBUG) -c $(CPPFLAGS) -I$(srcdir) -I$(basedir) -I../filed -I../dird -I../stored $(DINCLUDE) $(CFLAGS) $<

bpluginfo: Makefile bpluginfo.o
	$(LIBTOOL_LINK) $(CXX) $(LDFLAGS) -L../lib -o $@ bpluginfo.o -lbac $(DLIB) $(LIBS) $(GETTEXT_LIBS) $(OPENSSL_LIBS)

TESTDEDUPOBJS = ../stored/bitarray.o ../stored/dedupengine.o test-dedup.o

test-dedup.o: test-dedup.c
	$(CXX) $(DEFS) $(DEBUG) -c $(CPPFLAGS) $(TOKYOCABINET_INC) -I$(srcdir) -I$(basedir) -I../stored -I../lib $(DINCLUDE) $(CFLAGS) $<

test-dedup: Makefile $(TESTDEDUPOBJS) ../lib/libbaccfg$(DEFAULT_ARCHIVE_TYPE) ../lib/libbac$(DEFAULT_ARCHIVE_TYPE)
	$(LIBTOOL_LINK) $(CXX) $(LDFLAGS) -static -L../lib -o $@ $(TESTDEDUPOBJS) $(ZLIBS) \
	   -lbaccfg -lbac -lm $(TOKYOCABINET_LIBS) $(DLIB) $(LIBS) $(WRAPLIBS) \
	$(GETTEXT_LIBS) $(OPENSSL_LIBS) $(CAP_LIBS)


Makefile: $(srcdir)/Makefile.in $(topdir)/config.status
	cd $(topdir) \
	  && CONFIG_FILES=$(thisdir)/$@ CONFIG_HEADERS= $(SHELL) ./config.status

libtool-clean:
	@$(RMF) -r .libs _libs

clean:	libtool-clean
	@$(RMF) core core.* a.out *.o *.bak *~ *.intpro *.extpro 1 2 3 fs-io-error
	@$(RMF) $(DIRTOOLS)

realclean: clean
	@$(RMF) tags

distclean: realclean
	if test $(srcdir) = .; then $(MAKE) realclean; fi
	(cd $(srcdir); $(RMF) Makefile)

devclean: realclean
	if test $(srcdir) = .; then $(MAKE) realclean; fi
	(cd $(srcdir); $(RMF) Makefile)

install-debug-tags_test: debug-tags_test
	$(LIBTOOL_INSTALL) $(INSTALL_PROGRAM) debug-tags_test $(DESTDIR)$(sbindir)/

install-fs-io-error: fs-io-error
	$(INSTALL_PROGRAM) fs-io-error $(DESTDIR)$(sbindir)/fs-io-error

install-bjoblist: bjoblist
	$(LIBTOOL_INSTALL) $(INSTALL_PROGRAM) bjoblist $(DESTDIR)$(sbindir)/

install-bsnapshot: bsnapshot
	$(LIBTOOL_INSTALL) $(INSTALL_PROGRAM) bsnapshot $(DESTDIR)$(sbindir)/bsnapshot

installall:  $(TOOLS) timelimit
	@for tool in ${TOOLS} timelimit ; do \
	   $(LIBTOOL_INSTALL) $(INSTALL_PROGRAM) $$tool $(DESTDIR)$(sbindir)/$$tool ; \
	done
#	chattr +i $(DESTDIR)$(sbindir)/bsmtp
#	chmod 755 $(DESTDIR)$(sbindir)/bsmtp

install-cdp:
	$(MAKE) -C cdp-client install

# Allow non-root execution of bsmtp for non-root Directors
install: $(INSTOOLS)
	@for tool in ${INSTOOLS} ; do \
	   $(LIBTOOL_INSTALL) $(INSTALL_PROGRAM_ALL) $$tool $(DESTDIR)$(sbindir)/$$tool ; \
	done
#	chattr +i $(DESTDIR)$(sbindir)/bsmtp

install-unittests: $(INSTTESTS)
	@for tool in ${INSTTESTS} ; do \
	   $(LIBTOOL_INSTALL) $(INSTALL_PROGRAM) $$tool $(DESTDIR)$(sbindir)/$$tool ; \
	done

uninstall:
	@for tool in ${INSTOOLS} ${INSTTESTS} ; do \
	   $(RMF) $(DESTDIR)$(sbindir)/$$tool ; \
	done



# Semi-automatic generation of dependencies:
# Use gcc -MM because X11 `makedepend' doesn't work on all systems
# and it also includes system headers.
# `semi'-automatic since dependencies are generated at distribution time.

depend:
	@$(MV) Makefile Makefile.bak
	@$(SED) "/^# DO NOT DELETE:/,$$ d" Makefile.bak > Makefile
	@$(ECHO) "# DO NOT DELETE: nice dependency list follows" >> Makefile
	@$(CXX) -S -M $(CPPFLAGS) -I$(srcdir) -I$(basedir) -I../stored *.c >> Makefile
	@if test -f Makefile ; then \
	    $(RMF) Makefile.bak; \
	else \
	   $(MV) Makefile.bak Makefile; \
	   echo " ===== Something went wrong in make depend ====="; \
	fi

# -----------------------------------------------------------------------
# DO NOT DELETE: nice dependency list follows
